package jinyilw.swing.visuals.Icomp;

import jinyilw.swing.SwingTools;
import jinyilw.swing.i18n.ICSetting;
import jinyilw.swing.i18n.IString;
import jinyilw.swing.i18n.LanguageChanger;

import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import java.awt.Component;
import java.util.ArrayList;

public class ITabbedPane extends JTabbedPane implements LanguageChanger
{
	private static final long serialVersionUID = -1149720461879124750L;
	private final ArrayList<IString> iStrings = new ArrayList<>();
	private final ArrayList<Component> components = new ArrayList<>();

	public ITabbedPane()
	{
	}

	public ITabbedPane(int placement)
	{
		super(placement);
	}

	public JPanel createTabbedPanel(String title)
	{
		JPanel panel = SwingTools.createTransBorderPane();
		addTab(title, panel);
		return panel;
	}

	public JPanel createTabbedPanel(IString title)
	{
		JPanel panel = SwingTools.createTransBorderPane();
		if (title != null)
		{
			addTab(title.getI18nString(), panel);
			iStrings.add(title);
			components.add(panel);
		} else
			addTab("", panel);
		return panel;
	}

	@Override
	public void remove(Component c)
	{
		int i = components.indexOf(c);
		if (i > 0)
		{
			components.remove(i);
			iStrings.remove(i);
		}
		super.remove(c);
	}

	@Override
	public void changLanguage()
	{
		int si = getSelectedIndex(), i = 0;
		int index;
		for (Component c : components)
		{
			index = indexOfComponent(c);
			super.remove(c);
			insertTab(iStrings.get(i).getI18nString(), null, c, null, index);
			i++;
		}
		setSelectedIndex(si);
	}

	@Override
	public boolean needChangLanguage()
	{
		if (!iStrings.isEmpty())
			return iStrings.get(0).needChange();
		return false;
	}

	@Override
	public void setICSetting(ICSetting iSetting)
	{

	}

	@Override
	public ICSetting getICSetting()
	{
		return null;
	}

	@Override
	public void updateText(String text)
	{
	}
}
